package com.rogue.blog.exception;

import com.rogue.blog.model.vo.ResponseVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by 86101 on 2018-10-18.
 */
@ControllerAdvice(value = "com.rogue.blog.controller")
public class ExceptionHandle {
    private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResponseVO handle(Exception e) {
        ResponseVO responseVO = new ResponseVO();
        logger.error("An exception occurs.", e);
        responseVO.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
        responseVO.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.name());
        responseVO.setMessage(e.getMessage());
        return responseVO;
    }
}
